#include "except.h"
#include "../PRINT/my_printf.h"
#include "../UART/uart.h"
#include "../EXTI/exti.h"
#include "../VIC/vic_reg.h"
#include "../PUBLIC/test.h"

void except_und(u32 addr){
    printf("[%x] Triggered undefined exceptions!\r\n", addr);

    log_all();
}

void except_swi(u32 addr){
    u32 val;
    val = *(u32 *)addr & 0xFFFFFF;
    printf("[%x] Triggered SWI exceptions 0x%x\r\n", addr, val);
}

void except_pabort(u32 addr){
    printf("[%x] Triggered PAbort exceptions\r\n", addr);
}

void except_dabort(u32 addr){
    __asm__(
        "msr cpsr_c, #0xd3\n"
    );
    printf("[%x] Triggered DAbort exceptions\r\n", addr);
    
    log_all();

}

void except_irq(u32 addr){
    u32 fp, sp, cpsr;

    __asm__(
        "mov %0, fp\n"
        "mov %1, sp\n"
        "mrs %2, cpsr\n"
        :"=r"(fp), "=r"(sp), "=r"(cpsr)
    );
    printf("except_irq fp:%x sp:%x sp:%x\r\n", fp, sp, cpsr);
    printf("[%x] Triggered IRQ exceptions!\r\n", addr);
    if(VICIRQSTATUS_CH0){   // 具体的中断函数
        ((void (*)(void))VICADDRESS_CH0)();
        VICADDRESS_CH0 = 0X0;
    }else if(VICIRQSTATUS_CH1){
        ((void (*)(void))VICADDRESS_CH1)();
        VICADDRESS_CH1 = 0x0;
    }
    printf("Overed IRQ exceptions! %c\r\n", ' ');
}

